﻿using Microsoft.EntityFrameworkCore;
using Models.WebClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

namespace GatewayServerService.Data
{
    public class DBIotDeviceValueCurrent
    {



        /// <summary>
        /// 缓存增加 获取变量列表
        /// </summary>
        /// <param name="cguid"></param>
        /// <returns></returns>
        public List<iot_device_value_current> getValueCurrentList(string dguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            List<iot_device_value_current> list = ef.iot_device_value_current.Where(c => c.vc_d_guid == dguid).ToList();
            return list;
        }
        public iot_device_value_current getValueCurrentByKey(string dguid, string key)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            iot_device_value_current model = ef.iot_device_value_current.Where(c => c.vc_d_guid == dguid && c.vc_key == key).FirstOrDefault();

            return model;
        }

        public List<ViewDeviceCurrentValue> getViewDeviceCurrentValue(string dguid)
        {
            List<ViewDeviceCurrentValue> viewDeviceCurrentValues = new List<ViewDeviceCurrentValue>();
            ViewDeviceCurrentValue viewDeviceCurrentValue = new ViewDeviceCurrentValue();

            string getDeviceGuidsObject = UtilMemoryCacheHelper.get<string>("GateWayClood_getValueCurrentByProduct_" + dguid);
            List<string> devices = new List<string>();
            if (string.IsNullOrEmpty(getDeviceGuidsObject))
            {

                DataTable dt = getValueCurrentDataTable(dguid);
                foreach (DataRow dr in dt.Rows)
                {
                    viewDeviceCurrentValue = new ViewDeviceCurrentValue();
                    viewDeviceCurrentValue.modelNo = int.Parse(dr["d_p_model_no"].ToString());
                    viewDeviceCurrentValue.key = dr["vc_key"].ToString();
                    viewDeviceCurrentValue.value = dr["vc_value"].ToString();
                    viewDeviceCurrentValue.addtime = DateTime.Parse(dr["vc_add_time"].ToString());
                    viewDeviceCurrentValues.Add(viewDeviceCurrentValue);

                    devices.Add((dr["d_guid"].ToString() + dr["vc_key"].ToString()).ToLower());

                    string data = JsonConvert.SerializeObject(viewDeviceCurrentValue);
                    UtilMemoryCacheHelper.set("deviceValue_" + (dr["d_guid"].ToString() + dr["vc_key"].ToString()).ToLower(), data, 60);
                }
                string dataDevices = JsonConvert.SerializeObject(devices);
                UtilMemoryCacheHelper.set("GateWayClood_getValueCurrentByProduct_" + dguid, dataDevices, 30);
            }
            else
            {
                devices = JsonConvert.DeserializeObject<List<string>>(getDeviceGuidsObject);
                foreach (string deviceGuidkey in devices.Distinct().ToList())
                {
                    string data = UtilMemoryCacheHelper.get<string>("deviceValue_" + deviceGuidkey.ToLower());
                    //Console.WriteLine(deviceGuid + "--------------" + data);
                    if (!string.IsNullOrEmpty(data))
                    {
                        viewDeviceCurrentValue = new ViewDeviceCurrentValue();
                        viewDeviceCurrentValue = JsonConvert.DeserializeObject<ViewDeviceCurrentValue>(data);
                        viewDeviceCurrentValues.Add(viewDeviceCurrentValue);
                    }
                }
            }
            return viewDeviceCurrentValues;
        }

        public DataTable getValueCurrentDataTable(string dguid)
        {
            string sql = "select  d_guid,d_p_model_no,vc_key,vc_value,vc_add_time from iot_device_value_current c inner join iot_device on d_guid=vc_d_guid  where d_guid='" + dguid + "'";
            return MySqlYihongIot.GetDataTable(sql);
        }

    }
}
